﻿using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using GWB.Application.Service.PlatForm.IBK;
using GWB.Application.Service.PlatForm.Test;
using GWB.Application.Service.Startup.Dtos;
using GWB.Core.Common;
using GWB.Core.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using OfficeOpenXml;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using static GWB.Application.Service.PlatForm.IBK.IBKServer;
using static GWB.Core.Common.DingDingHelper;

namespace GWB.Application.Service.Startup
{
    public class StartupServer : IStartupServer, ITransient, IDynamicApiController
    {
        private readonly IIBKServer _IBKServer;
        private readonly ITest1Server _ITestServer;

        public StartupServer(IIBKServer IBKServer, ITest1Server ITestServer)
        {
            _IBKServer = IBKServer;
            _ITestServer = ITestServer;
        }
        public void Startup()
        {

            var taskA = Task.Run(() =>
            {
                var count = SqlSugarHelper.Db.Queryable<CarNoModel>().Count();
                Task.Run(() =>
                {
                    Console.WriteLine("启动1");
                    var pageIndex = 1; // pagenumber是从1开始的不是从零开始的
                    var pageSize = 1000;
                    var totalCount = 0;
                    if (count > 1000 * 10)
                    {
                        pageSize = count / 10;
                    }
                    var date1 = SqlSugarHelper.Db.Queryable<CarNoModel>()
                        .ToPageList(pageIndex, pageSize, ref totalCount);
                    foreach (var item in date1)
                    {
                        Task.Run(() =>
                        {
                            _IBKServer.IbkStartup(item.CarNo);
                        });
                    }

                });
                Task.Run(() =>
                {
                    Console.WriteLine("启动2");
                    var pageIndex = 2; // pagenumber是从1开始的不是从零开始的
                    var pageSize = count / 10;
                    var totalCount = 0;
                    var date1 = SqlSugarHelper.Db.Queryable<CarNoModel>()
                        .ToPageList(pageIndex, pageSize, ref totalCount);
                    foreach (var item in date1)
                    {
                        Task.Run(() =>
                        {
                            _IBKServer.IbkStartup(item.CarNo);
                        });
                    }

                });
                Task.Run(() =>
                {
                    Console.WriteLine("启动3");
                    var pageIndex = 3; // pagenumber是从1开始的不是从零开始的
                    var pageSize = count / 10;
                    var totalCount = 0;
                    var date1 = SqlSugarHelper.Db.Queryable<CarNoModel>()
                        .ToPageList(pageIndex, pageSize, ref totalCount);
                    foreach (var item in date1)
                    {
                        Task.Run(() =>
                        {
                            _IBKServer.IbkStartup(item.CarNo);
                        });
                    }

                });
                Task.Run(() =>
                {
                    Console.WriteLine("启动4");
                    var pageIndex = 4; // pagenumber是从1开始的不是从零开始的
                    var pageSize = count / 10;
                    var totalCount = 0;
                    var date1 = SqlSugarHelper.Db.Queryable<CarNoModel>()
                        .ToPageList(pageIndex, pageSize, ref totalCount);
                    foreach (var item in date1)
                    {
                        Task.Run(() =>
                        {
                            _IBKServer.IbkStartup(item.CarNo);
                        });
                    }

                });
                Task.Run(() =>
                {

                    Console.WriteLine("启动5");
                    var pageIndex = 5; // pagenumber是从1开始的不是从零开始的
                    var pageSize = count / 10;
                    var totalCount = 0;
                    var date1 = SqlSugarHelper.Db.Queryable<CarNoModel>()
                        .ToPageList(pageIndex, pageSize, ref totalCount);
                    foreach (var item in date1)
                    {
                        Task.Run(() =>
                        {
                            _IBKServer.IbkStartup(item.CarNo);
                        });
                    }

                });
                Task.Run(() =>
                {
                    Console.WriteLine("启动6");
                    var pageIndex = 6; // pagenumber是从1开始的不是从零开始的
                    var pageSize = count / 10;
                    var totalCount = 0;
                    var date1 = SqlSugarHelper.Db.Queryable<CarNoModel>()
                        .ToPageList(pageIndex, pageSize, ref totalCount);
                    foreach (var item in date1)
                    {
                        Task.Run(() =>
                        {

                            _IBKServer.IbkStartup(item.CarNo);
                        });
                    }

                });
                Task.Run(() =>
                {
                    Console.WriteLine("启动7");
                    var pageIndex = 7; // pagenumber是从1开始的不是从零开始的
                    var pageSize = count / 10;
                    var totalCount = 0;
                    var date1 = SqlSugarHelper.Db.Queryable<CarNoModel>()
                        .ToPageList(pageIndex, pageSize, ref totalCount);
                    foreach (var item in date1)
                    {
                        Task.Run(() =>
                        {

                            _IBKServer.IbkStartup(item.CarNo);
                        });
                    }

                });
                Task.Run(() =>
                {
                    Console.WriteLine("启动8");
                    var pageIndex = 8; // pagenumber是从1开始的不是从零开始的
                    var pageSize = count / 10;
                    var totalCount = 0;
                    var date1 = SqlSugarHelper.Db.Queryable<CarNoModel>()
                        .ToPageList(pageIndex, pageSize, ref totalCount);
                    foreach (var item in date1)
                    {
                        Task.Run(() =>
                        {

                            _IBKServer.IbkStartup(item.CarNo);
                        });
                    }

                });
                Task.Run(() =>
                {
                    Console.WriteLine("启动9");
                    var pageIndex = 9; // pagenumber是从1开始的不是从零开始的
                    var pageSize = count / 10;
                    var totalCount = 0;
                    var date1 = SqlSugarHelper.Db.Queryable<CarNoModel>()
                        .ToPageList(pageIndex, pageSize, ref totalCount);
                    foreach (var item in date1)
                    {
                        Task.Run(() =>
                        {

                            _IBKServer.IbkStartup(item.CarNo);
                        });
                    }

                });
                Task.Run(() =>
                {
                    Console.WriteLine("启动10");
                    var pageIndex = 10; // pagenumber是从1开始的不是从零开始的
                    var pageSize = count / 10;
                    var totalCount = 0;
                    var date1 = SqlSugarHelper.Db.Queryable<CarNoModel>()
                        .ToPageList(pageIndex, pageSize, ref totalCount);
                    foreach (var item in date1)
                    {
                        Task.Run(() =>
                        {
                            _IBKServer.IbkStartup(item.CarNo);
                        });
                    }

                });
            });

        }

        /// <summary>
        /// 数据库初始化
        /// </summary>
        public void Database()
        {
            var types = Assembly.Load("GWB.Core").GetTypes()
                .Where(x => x.GetCustomAttribute<SugarTable>() != null
                            && x.Namespace == "GWB.Core.Model").ToArray();
            SqlSugarHelper.Db.CodeFirst.InitTables(types);

        }


        /// <summary>
        /// 获取分页
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultDto> GetCarNoPage(InputDto inputDto)
        {
            RefAsync<int> total = 0;
            var data = await SqlSugarHelper.Db.Queryable<CarNoModel>()
                .ToPageListAsync(inputDto.PageIndex, inputDto.PageSize, total);
            return new ResultDto()
            {
                Data = data,
                Total = total
            };
        }

        /// <summary>
        /// 获取错误分页
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultDto> GetErrorLogPage(InputDto inputDto)
        {
            RefAsync<int> total = 0;
            var data = await SqlSugarHelper.Db.Queryable<ErrorLogEntity>()
                .ToPageListAsync(inputDto.PageIndex, inputDto.PageSize, total);
            return new ResultDto()
            {
                Data = data,
                Total = total
            };
        }

        /// <summary>
        /// 导入数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> ImportData(IFormFile file)
        {

            ReadExcel(file);
            return true;
        }


        /// <summary>
        /// 读取Excel表格内容
        /// </summary>
        private void ReadExcel(IFormFile file)
        {

            // 1.配置Eplus
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial; // 使用非商业许可，如有需要请更改  
            ExcelPackage package1 = new ExcelPackage(file.OpenReadStream());

            ExcelWorksheet worksheet = package1.Workbook.Worksheets[0];  // 选择第一个工作表  
            int rowCount = worksheet.Dimension.Rows;  // 获取行数  
            int colCount = worksheet.Dimension.Columns;  // 获取列数 
            var list = new List<CarNoModel>();
            for (int i = 1; i <= rowCount; i++)
            {
                for (int j = 1; j <= colCount; j++)
                {
                    var value = worksheet.Cells[i, j].Value;
                    if (value!=null&&!string.IsNullOrEmpty(value.ToString()))
                    {
                        list.Add(new CarNoModel()
                        {
                            CarNo = value.ToString()
                        });
                    }

                }
            }
            //删除数据库
            SqlSugarHelper.Db.Deleteable<CarNoModel>().ExecuteCommand();
            SqlSugarHelper.Db.Insertable<CarNoModel>(list).ExecuteCommandAsync();


        }

    }
}
